<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <title>航班添加</title>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
    <script type="text/javascript">
        $(function () {
            citySelect();
            $("#flightNo").blur(function () {
                $.post(
                    "flight/check",
                    {flightNo:$("#flightNo").val()},
                    function (data) {
                        if(data.status==1){
                            $("#flightNo").next().html(data.info);
                        }else{
                            $("#flightNo").next().html('');
                        }
                    },"json"
                )
            })

            $("form").submit(function () {
                var departureCity=$("#departureCity").val();
                var arrivalCity=$("#arrivalCity").val();
                if(departureCity==""||arrivalCity==""){
                    alert('请选择起飞城市和到达城市');
                    return false;
                }else if(departureCity==arrivalCity){
                    alert('起飞城市和达到城市不能相同');
                    return false;
                }else if(/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (0[1-9]|1[0-9]|2[0-4]):(0[1-9]|[1-5][0-9]):(0[1-9]|[1-5][0-9])$/.test($("#departureTime").val())==false||
                    /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (0[1-9]|1[0-9]|2[0-4]):(0[1-9]|[1-5][0-9]):(0[1-9]|[1-5][0-9])$/.test($("#arrivalTime").val())==false){
                    alert('时间格式不正确');
                    alert($("#departureTime").val());alert($("#arrivalTime").val());

                    return false;
                }else {
                    $.post(
                        "flight/add",
                        $("form").serialize(),
                        function (data) {
                            alert(data.info);
                            if(data.status==1){
                                location.href='index.jsp';
                            }else{
                                location.href='add.jsp';
                            }
                        }
                    )
                }
                return false;
            })
        })

        function citySelect() {
            $.post(
                "city/getAll",
                {},
                function (data) {
                    $("#departureCity option:gt(0)").remove();
                    $("#arrivalCity option:gt(0)").remove();
                    $.each(data,function (index,item) {
                        var option='<option value='+item.id+'>'+item.cityName+'</option>';
                        $("#departureCity").append(option);
                        $("#arrivalCity").append(option);
                    })
                },"json"
            )
        }
    </script>
</head>
<body>
    <h1>航班添加页面</h1>
   <form>
       航班编号：<input name="flightNo" id="flightNo"/><span id="check"></span><br>
       起飞城市：<select id="departureCity" name="departureCity.id">
                       <option value="">--请选择起飞城市--</option>
                   </select>
       起飞时间：<input name="departureTime" id="departureTime"/><br>
       到达城市：<select id="arrivalCity" name="arrivalCity.id">
                       <option value="">--请选择到达城市--</option>
                   </select>
       到达时间：<input name="arrivalTime" id="arrivalTime"/><br>
       <input type="submit" value="添加"/>
   </form>
</body>
</html>
